package com.kly.sc.api.application.controller.order.rsp;

import com.alibaba.fastjson.JSONObject;
import com.kly.dto.ProductDto;
import com.kly.dto.TOrderDto;
import com.kly.enums.PurchaseType;
import com.kly.sc.api.infras.utils.StringUtils;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;

/**
 * @author linqu
 */
public class GenerateOrderResp {
    private String orderNo;
    private String userCode;
    private String productCode;
    private Integer productType;
    private BigDecimal payableAmount;
    private BigDecimal paidAmount;
    private Integer orderType;
    /**
     * 商品类型: 1-消耗类 2-订阅类
     */
    private Integer purchaseType;
    /**
     * 商品类型补充：
     * 1-正常一次性消耗类商品，
     * 2-app封装成会员订阅类的一次性商品（app展示成会员，但无须续订，支付上设计成一次性消耗类商品进行处理）
     */
    private Integer purchaseTypeExt;

    private String purchaseProductName;
    /**
     * 当前订单支付令牌
     */
    private String purchaseToken;
    /**
     * 最近一笔支付成功的订单支付令牌
     */
    private String latestPaidOrderPurchaseToken;
    /**
     *商品促销折扣码
     */
    private String discountCode;
    /**
     * 显示支付错误开关
     */
    private Boolean orderPayFailedSwitch;

    private Integer orderPayTimeOut;

    public Boolean getOrderPayFailedSwitch() {
        return orderPayFailedSwitch;
    }

    public void setOrderPayFailedSwitch(Boolean orderPayFailedSwitch) {
        this.orderPayFailedSwitch = orderPayFailedSwitch;
    }

    public String getPurchaseToken() {
        return purchaseToken;
    }

    public void setPurchaseToken(String purchaseToken) {
        this.purchaseToken = purchaseToken;
    }

    public String getLatestPaidOrderPurchaseToken() {
        return latestPaidOrderPurchaseToken;
    }

    public void setLatestPaidOrderPurchaseToken(String latestPaidOrderPurchaseToken) {
        this.latestPaidOrderPurchaseToken = latestPaidOrderPurchaseToken;
    }

    public String getPurchaseProductName() {
        return purchaseProductName;
    }

    public void setPurchaseProductName(String purchaseProductName) {
        this.purchaseProductName = purchaseProductName;
    }

    public String getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public String getUserCode() {
        return userCode;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }

    public String getProductCode() {
        return productCode;
    }

    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }

    public Integer getProductType() {
        return productType;
    }

    public void setProductType(Integer productType) {
        this.productType = productType;
    }

    public BigDecimal getPayableAmount() {
        return payableAmount;
    }

    public void setPayableAmount(BigDecimal payableAmount) {
        this.payableAmount = payableAmount;
    }

    public BigDecimal getPaidAmount() {
        return paidAmount;
    }

    public void setPaidAmount(BigDecimal paidAmount) {
        this.paidAmount = paidAmount;
    }

    public Integer getOrderType() {
        return orderType;
    }

    public void setOrderType(Integer orderType) {
        this.orderType = orderType;
    }

    public Integer getPurchaseType() {
        return purchaseType;
    }

    public void setPurchaseType(Integer purchaseType) {
        this.purchaseType = purchaseType;
    }

    public String getDiscountCode() {
        return discountCode;
    }

    public void setDiscountCode(String discountCode) {
        this.discountCode = discountCode;
    }

    public Integer getPurchaseTypeExt() {
        return purchaseTypeExt;
    }

    public void setPurchaseTypeExt(Integer purchaseTypeExt) {
        this.purchaseTypeExt = purchaseTypeExt;
    }

    public Integer getOrderPayTimeOut() {
        return orderPayTimeOut;
    }

    public void setOrderPayTimeOut(Integer orderPayTimeOut) {
        this.orderPayTimeOut = orderPayTimeOut;
    }

    public static GenerateOrderResp toVo(TOrderDto order, ProductDto productDto,
                                         Integer purchaseType,
                                         String amountThresholdValue,
                                         Boolean orderPayFailedSwitch,
                                         Integer orderPayTimeOut) {
        GenerateOrderResp resp = new GenerateOrderResp();
        BeanUtils.copyProperties(order, resp);
        resp.setPurchaseType(productDto.getPurchaseType());

        final String productConfig = productDto.getProductConfig();


        if (productDto.getPurchaseType().equals(purchaseType)) {

            if (StringUtils.isNotEmpty(productConfig)) {
                final JSONObject config = JSONObject.parseObject(productConfig);
                resp.setPurchaseProductName(config.getString("purchaseProductName"));
            }
            resp.setDiscountCode(productDto.getDiscountCode());
        }

        // 一次消耗商品扩展：purchaseTypeExt=2时，google play上配置成一次性商品，但app上需要以会员订阅商品性质进行展示
        if (order.getPurchaseType() != null && order.getPurchaseType().equals(PurchaseType.ONCE_PRODUCT.getType())) {
            final JSONObject config = JSONObject.parseObject(productConfig);
            resp.setPurchaseProductName(config.getString("purchaseProductName"));
            final Integer purchaseTypeExt = config.getInteger("purchaseTypeExt");
            resp.setPurchaseTypeExt(purchaseTypeExt == null ? PurchaseType.ONCE_PRODUCT.getType() : purchaseTypeExt);
        }

        if (order.getPayableAmount() != null && StringUtils.isNotEmpty(amountThresholdValue)) {
            resp.setPayableAmount(order.getPayableAmount().multiply(new BigDecimal(amountThresholdValue)));
        }
        resp.setOrderPayFailedSwitch(orderPayFailedSwitch);
        resp.setOrderPayTimeOut(orderPayTimeOut);
        return resp;
    }
}
